package ywpt.web.action.people;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.PeopleFamily;
import ywpt.service.people.FamilyService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class FamilyAction extends BaseAction implements
ModelDriven<PeopleFamily>, Preparable{

	/**
	 * ��ͥ��Ա
	 */
	private static final long serialVersionUID = 831979737039360330L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private Long peopleId;
	
	private PeopleFamily familyInfo;
	private List<PeopleFamily> familyInfoList = new  ArrayList<PeopleFamily>();
	private FamilyService familyService ;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public PeopleFamily getFamilyInfo() {
		return familyInfo;
	}

	public void setFamilyInfo(PeopleFamily familyInfo) {
		this.familyInfo = familyInfo;
	}

	public List<PeopleFamily> getFamilyInfoList() {
		return familyInfoList;
	}

	public void setFamilyInfoList(List<PeopleFamily> familyInfoList) {
		this.familyInfoList = familyInfoList;
	}


	public FamilyService getFamilyService() {
		return familyService;
	}

	public void setFamilyService(FamilyService familyService) {
		this.familyService = familyService;
	}

	//**************************************************************
	@Override
	public PeopleFamily getModel() {
		return this.familyInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.familyInfo = familyService.getFamilyInfoById(id);
		}else{
			this.familyInfo= new PeopleFamily();
		}
	}
	//************************************************************
	public String list() throws Exception{
		DetachedCriteria query = DetachedCriteria.forClass(PeopleFamily.class);
		query.createAlias("people", "people").add(Restrictions.eq("people.id", peopleId));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = familyService.getFamilyInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		familyInfoList.addAll(familyService.getFamilyInfos(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		this.setPeopleId(peopleId);
		return INPUT;
	}
	public String edit() 
	{
		familyInfo=this.familyService.getFamilyInfoById(id);
		this.setPeopleId(familyInfo.getPeople().getId());
		return INPUT;
	}
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() throws Exception {	

		try {
			if (id == null) 
			{
				familyService.saveFamilyInfo(familyInfo,peopleId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
				return SUCCESS;
			} 
			else 
			{   				
				familyService.updateFamilyInfo(familyInfo,peopleId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				return SUCCESS;					
			}
		} 
		catch (UniqueConstraintException e) 
		{	
			this.addActionError(e.getMessage());
			return INPUT;
		}
	}	
	/**
	 * ɾ��
	 * 
	 * @return
	 */
	public String delete() throws Exception {		
		if (id != null) 
		{	
			familyService.removeFamilyInfo(id);
			return SUCCESS;
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		
	}
}
